package com.retail.xiaocui.server.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import com.retail.common.core.domain.Result;
import com.retail.common.core.utils.DateUtils;
import com.retail.common.core.web.page.TableDataInfo;
import com.retail.system.common.domain.SysUser;
import com.retail.system.remote.RemoteUserService;
import com.retail.xiaocui.common.domain.Procurement;
import com.retail.xiaocui.common.domain.req.AddPurchaseReq;
import com.retail.xiaocui.common.domain.req.PurchaseReq;
import com.retail.xiaocui.server.service.ProcurementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.retail.xiaocui.server.mapper.PurchaseMapper;
import com.retail.xiaocui.common.domain.Purchase;
import com.retail.xiaocui.server.service.PurchaseService;

/**
 * 采购申请单Service业务层处理
 *
 * @author retail
 * @date 2024-03-05
 */
@Service
public class PurchaseServiceImpl implements PurchaseService
{
    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private ProcurementService procurementService;

    @Autowired
    private RemoteUserService remoteUserService;








    /**
     * 查询采购申请单
     *
     * @param procurementId 采购申请单主键
     * @return 采购申请单
     */
    @Override
    public Purchase selectPurchaseByProcurementId(Integer procurementId)
    {
        return purchaseMapper.selectPurchaseByProcurementId(procurementId);
    }

    /**
     * 查询采购申请单列表
     *
     * @param purchase 采购申请单
     * @return 采购申请单
     */
    @Override
    public List<Purchase> selectPurchaseList(Purchase purchase)
    {
        return purchaseMapper.selectPurchaseList(purchase);
    }

    /**
     * 新增采购申请单
     *
     * @param purchase 采购申请单
     * @return 结果
     */
    @Override
    public int insertPurchase(Purchase purchase)
    {
        //新增的
        return purchaseMapper.insertPurchase(purchase);
    }

    /**
     * 修改采购申请单
     *
     * @param purchase 采购申请单
     * @return 结果
     */
    @Override
    public int updatePurchase(Purchase purchase)
    {
        purchase.setUpdateTime(DateUtils.getNowDate());
        return purchaseMapper.updatePurchase(purchase);
    }

    /**
     * 批量删除采购申请单
     *
     * @param procurementIds 需要删除的采购申请单主键
     * @return 结果
     */
    @Override
    public int deletePurchaseByProcurementIds(Integer[] procurementIds)
    {
        return purchaseMapper.deletePurchaseByProcurementIds(procurementIds);
    }

    /**
     * 删除采购申请单信息
     *
     * @param procurementId 采购申请单主键
     * @return 结果
     */
    @Override
    public int deletePurchaseByProcurementId(Integer procurementId)
    {
        return purchaseMapper.deletePurchaseByProcurementId(procurementId);
    }

    @Override
    public List<Purchase> purchaseList() {
        return purchaseMapper.purchaseList();
    }


    @Override
    public Result addPurchase(AddPurchaseReq addPurchaseReq) {

            //返回一个id集合
            List<Integer> list = addPurchaseReq.getIds();
            //将bigDecimal 设置默认0
            AtomicReference<BigDecimal> sumRef = new AtomicReference<>(BigDecimal.ZERO);
            //循环遍历拿到每一个值
            list.forEach(integer -> {
                //每一次查询会根据Id去查询到对应的对象
                Procurement procurement = procurementService.selectProId(integer);
                //判断非null的情况下
                if (procurement != null) {
                    //拿到价格
                    BigDecimal procurementPrice = procurement.getProcurementPrice();

                    if(procurementPrice!=null){
                        sumRef.set(sumRef.get().add(procurementPrice)); // 使用AtomicReference进行累加
                    }
                }
            });
            BigDecimal sum = sumRef.get(); // 获取最终的累加结果
            System.out.println("累加结果为: " + sum);
            addPurchaseReq.setBigDecimal(sum);
            //判断前台是否传采购单id 如果传了就加上采购单的id
            if(null!=addPurchaseReq.getProcurementId()){
                   //通过采购单的Id去查询对应的对象,将状态修改成为未分配状态
                addPurchaseReq.setStatus(2);
                purchaseMapper.updpurchase(addPurchaseReq);
                //给需求单中的id给到采购单的id
                procurementService.updatePurchase(addPurchaseReq);
            }
            //如果前台没有选采购id 那就创建一个采购单id
            if(null==addPurchaseReq.getProcurementId()){
                Purchase purchase = new Purchase();
                purchase.setProcurementStatus(2);
                purchase.setProcurementSum(sum);
    //            addPurchaseReq.setStatus(2);
                //前台只传需求申请单的Ids 一个或者多个  给一个状态
                purchaseMapper.addPurchase(purchase);
                //还要获取到采购单的主键Id 给需求申请单(一个或多个)
                //将返回的主键id赋值
                addPurchaseReq.setProcurementId(purchase.getProcurementId());
                //进行修改
                procurementService.updatePurchase(addPurchaseReq);
            }

        return Result.success();
    }



    @Override
    public Result userList() {
        TableDataInfo list = remoteUserService.list(new SysUser());
        return Result.success(list);
    }

    @Override
    public Result userUpdatePurchase(PurchaseReq purchaseReq) {
        //分配的时候查一下id 是否为未分配状态 其他的都是已分配
        Integer purchaseId = purchaseReq.getPurchaseId();
        //根据采购单id去查询对象
      Purchase purchase=  purchaseMapper.findBypurchaseId(purchaseId);

      if(purchase.getProcurementStatus()==1){
          return Result.error("新建状态,不能进行分配");
      }

      if(purchase.getProcurementStatus()!=2){
          return Result.error("已分配完毕,不能再进行分配了");
      }
        int res = purchaseMapper.userUpdatePurchase(purchaseReq);
            return Result.success(res,"分配完毕");
    }
}
